home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / WWW / MailRobot / Implementation / subscribe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-18  |  3.7 KB  |  164 lines

  1. /*    Handle subscription/unsubscription from mailing lists
  2. **    -----------------------------------------------------
  3. */
  4. #include "listserv.h"
  5. static char rcsid[] = "$Header: /tempf/aem/system/listserv/RCS/subscribe.c,v 1.2 91/06/18 14:08:02 aem Exp $";
  6.  
  7. extern FILE *msg;
  8. extern FILE *mailer;
  9.  
  10. subscription(from,command,add)
  11. char *from,*command;
  12. int add;
  13.     {
  14.     FILE *list;
  15.     FILE *listtmp;
  16.     char grp[256];
  17.     char adr[256];
  18.     char tmp[256];
  19.     char buf[BUFSIZ];
  20.     int del = 0;
  21.     int i;
  22.  
  23.     printf("subscription %s %s %d\n", from, command, add);
  24.  
  25.     i = sscanf(command,"%s%s%s", tmp, adr, grp);
  26.     if (i < 2 || i > 3)
  27.         {
  28.         callmailer("", from, command);
  29.         fprintf(mailer,"Your command\n");
  30.         fprintf(mailer,"\t%s\n", command);
  31.         fprintf(mailer,"could not be understood.  Correct syntax is\n");
  32.         fprintf(mailer,"\tADD listname\n");
  33.         fprintf(mailer,"\tADD mailbox listname\n");
  34.         fprintf(mailer,"\tDELETE listname\n");
  35.         fprintf(mailer,"\tDELETE mailbox listname\n");
  36.         fflush(mailer);
  37.         pclose(mailer);
  38.         return(-1);
  39.         }
  40.  
  41.     if (i == 2)
  42.         {
  43.         strcpy(grp, adr);
  44.         strcpy(adr, from);
  45.         }
  46.     
  47.     cleanup(grp);
  48.     cleanup(adr);
  49.  
  50.     sprintf(tmp,"%s/%s.info", SERVDIR, grp);
  51.     if (access(tmp,R_OK) != 0)
  52.         {
  53.         callmailer("", from, "");
  54.         fprintf(mailer,"The mailing list \"%s\" could not be found.\n",
  55.             grp);
  56.         fprintf(mailer,"You may use the INDEX command to get a listing\n");
  57.         fprintf(mailer,"of available mailing lists.\n");
  58.         fflush(mailer);
  59.         pclose(mailer);
  60.         return(-1);
  61.         }
  62.  
  63.     strcpy(tmp, SERVDIR);
  64.     strcat(tmp, "/");
  65.     strcat(tmp, grp);
  66.     if (add)
  67.         {
  68.         list = fopen(tmp, "a");
  69.         if (list == NULL)
  70.             {
  71.             callmailer("Postmaster", from, "");
  72.             fprintf(mailer,"Couldn't add per request. (Error[1]) Please try later.\n");
  73.             fprintf(mailer,">%s\n", command);
  74.             fflush(mailer);
  75.             pclose(mailer);
  76.             return(-1);
  77.             }
  78.         flock(fileno(list), LOCK_EX);
  79.         fprintf(list, "%s\n", adr);
  80.         fflush(list);
  81.         flock(fileno(list), LOCK_UN);
  82.         fclose(list);
  83.         }
  84.     else
  85.         {
  86.         del = 0;
  87.         list = fopen(tmp, "r");
  88.         if (list == NULL)
  89.             {
  90.             callmailer("Postmaster", from, "");
  91.             fprintf(mailer,"Couldn't read subscription list.  (Error[2]). Please try later.\n");
  92.             fprintf(mailer,">%s\n", command);
  93.             fflush(mailer);
  94.             pclose(mailer);
  95.             return(-1);
  96.             }
  97.         flock(fileno(list), LOCK_EX);
  98.         fprintf(list, "%s\n", adr);
  99.  
  100.         strcpy(tmp, SERVDIR);
  101.         strcat(tmp, "/");
  102.         strcat(tmp, grp);
  103.         strcat(tmp, ".tmp");
  104.         listtmp = fopen(tmp, "w");
  105.         if (listtmp == NULL)
  106.             {
  107.             callmailer("Postmaster", from, "");
  108.             fprintf(mailer,"Couldn't write subscription list (Error[3]). Please try later.\n");
  109.             fprintf(mailer,">%s\n", command);
  110.             fflush(mailer);
  111.             pclose(mailer);
  112.             return(-1);
  113.             }
  114.         /* copy the list, omitting the one address */
  115.         while (fgets(buf, sizeof(buf), list))
  116.             {
  117.             buf[strlen(buf)-1] = '\0';
  118.             if (strcasecmp(buf, adr))
  119.                 {
  120.                 fputs(buf, listtmp);
  121.                 fputs("\n", listtmp);
  122.                 }
  123.             else
  124.                 del++;
  125.             }
  126.         fflush(listtmp);
  127.         fclose(listtmp);
  128.  
  129.         /* replace the old list with the shortened one */
  130.         strcpy(buf, SERVDIR);
  131.         strcat(buf, "/");
  132.         strcat(buf, grp);
  133.         unlink(buf);    /* delete original file */
  134.         rename(tmp, buf);    /* put updated one in place */
  135.         flock(fileno(list), LOCK_UN);    /* release lock */
  136.         }
  137.  
  138.     if (strcmp(from, adr))
  139.         {
  140.         callmailer(adr, from, command);
  141.         fprintf(mailer,"Per request by %s\n", from);
  142.         }
  143.     else
  144.         {
  145.         callmailer("", from, command);
  146.         fprintf(mailer,"Per your request\n");
  147.         }
  148.     fprintf(mailer,"\t\"%s\"\n", command);
  149.     if (add)
  150.         fprintf(mailer,"'%s' was ADDED to the '%s' mailing list.\n",
  151.             adr, grp);
  152.     else
  153.         if (del)
  154.             fprintf(mailer,
  155.             "'%s' was DELETED from the '%s' mailing list.\n",
  156.                 adr, grp);
  157.         else
  158.             fprintf(mailer,
  159.             "'%s' was NOT FOUND on the '%s' mailing list.\n",
  160.                 adr, grp);
  161.     fflush(mailer);
  162.     pclose(mailer);
  163.     }
  164.